package com.dxl.dao.account;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.dxl.common.enums.State;
import com.dxl.common.model.Page;
import com.dxl.common.utils.SqlBuilder;
import com.dxl.common.utils.Utils;
import com.dxl.dao.common.BaseDao;
import com.dxl.entity.account.Function;

/**
 * 功能集管理DAO
 * @author 邓小林
 */
@Repository
public class FunctionDao extends BaseDao {
	private static final Logger LOG = Logger.getLogger(FunctionDao.class);
	
	public Page<Function> list(String[] parentIds, String keyword, Integer state, Integer pageNo, Integer pageSize) {
		SqlBuilder sb = new SqlBuilder("select * from `function` where 1 = 1");
		if (Utils.isNotEmpty(parentIds)) {
			sb.appendStatement(" and parent_id in ").appendParamValues(parentIds);
		}
		if (Utils.isNotBlank(keyword)) {
			keyword = keyword.trim();
			sb.appendStatement(" and (");
			sb.appendStatement(" name like ").appendParamValue("%" + keyword + "%");
			sb.appendStatement(" or code like ").appendParamValue("%" + keyword + "%");
			sb.appendStatement(")");
		}
		if (Utils.isNotNull(state)) {
			sb.appendStatement(" and state = ").appendParamValue(state);
		}
		sb.appendStatement(" order by created_time desc");
		return this.list(Function.class, sb.toStatement(), sb.toParams(), pageNo, pageSize);
	}
	
	public List<Function> listFunctions(String accountId, State state) {
		SqlBuilder sb = new SqlBuilder("select e.* from `account` a, `account_role` b, `role` c, `role_function` d, `function` e where a.id = b.account_id and b.role_id = c.id and c.id = d.role_id and d.function_id = e.id");
		sb.appendStatement(" and a.id = ").appendParamValue(accountId);
		if (Utils.isNotNull(state)) {
			sb.appendStatement(" and a.state = ").appendParamValue(state.getState());
			sb.appendStatement(" and b.state = ").appendParamValue(state.getState());
			sb.appendStatement(" and c.state = ").appendParamValue(state.getState());
			sb.appendStatement(" and d.state = ").appendParamValue(state.getState());
			sb.appendStatement(" and e.state = ").appendParamValue(state.getState());
		}
		return this.list(Function.class, sb.toStatement(), sb.toParams());
	}
}
